package com.itm.lqm.dao;

import java.util.List;




import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.itm.lqm.entity.Teacher;
import com.itm.lqm.util.HibernateUtil;

public class TeacherDao {
	//查询所有Teacher
	public  List<Teacher> findAllTeacher() {
		Session session = HibernateUtil.openSession();
		Transaction tran = session.beginTransaction();
		Query query = session.createQuery("from Teacher");
		List<Teacher> list = query.list();
		
		for (Teacher em : list) {
			System.out.println("dao"+em);
		}
		return list;

		// TODO Auto-generated method stub

	}

	
	
	// 按照tid查询教师
		public Teacher findTeacherByTid(int tid) {

			Session session = HibernateUtil.openSession();
			Transaction tran = session.beginTransaction();

			Query query = session.createQuery("from Teacher where tid=?");
//			query.setString(0, sno);
			query.setInteger(0, tid);

			Teacher student =(Teacher) query.uniqueResult();
			return student;
		}

		/*
		 * 验证该教师是否存在<根据sid>
		 */
		public boolean isExistTid(int tid) {
			return findTeacherByTid(tid) == null ? false : true;
		}

		// 添加学生
		public boolean addTeacher(Teacher t) {
			Session session = null;
			Transaction tran = null;
			try {
				session = HibernateUtil.openSession();
				tran = session.beginTransaction();
				// ---------------------------------
				session.save(t);
				tran.commit();
			} catch (Exception e) {
				e.printStackTrace();
				tran.rollback();
			} finally {
				if (null != session) {
					session.close();
				}
			}
			return false;
		}
		//删除学生
		public boolean deleteTeacher(int tid) {
			Session session = null;
			Transaction tran = null;
			try {
				session = HibernateUtil.openSession();
				tran = session.beginTransaction();
				// ---------------------------------

				// 查找Teacher对象
				Teacher s = session.load(Teacher.class,tid);
				session.delete(s);
				// ---------------------------------
				tran.commit();
			} catch (Exception e) {
				e.printStackTrace();
				tran.rollback();
			} finally {
				if (null != session) {
					session.close();
				}
			}
			return false;
		}
		// 修改学生
		public  boolean updateTeacher(Teacher t) {
			Session session = null;
			Transaction tran = null;
			try {
				session = HibernateUtil.openSession();
				tran = session.beginTransaction();
				// ---------------------------------

				// 查找Teacher对象
				
				Query query = session.createQuery("update Teacher set tno=?,tname=?, role=? ,sex=? where tid=?");
				query.setParameter(0,t.getTno());
				query.setParameter(1,t.getTname());
				query.setParameter(2,t.getRole());
				query.setParameter(3,t.getSex());
				query.setParameter(4,t.getTid());
				
				return query.executeUpdate()>0;
				// ---------------------------------
				//tran.commit();
			} catch (Exception e) {
				e.printStackTrace();
				//tran.rollback();
			} finally {
				if (null != session) {
					session.close();
				}
			}
			return false;
		}
	
	
	

	


}
